package com.tg.app.adapter;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.tg.data.http.entity.ShareUser;
import com.tg.app.view.SwipeMenuLayout;
import com.tg.app.R;

import java.util.List;

/**
 * Describe:
 * Created by $USER_NAME on 2018/11/28.
 */
public class DeviceShareAdapter extends RecyclerView.Adapter<DeviceShareAdapter.ShareViewHolder>  {

    private Context  context ;
    private List<ShareUser> users;

    public   DeviceShareAdapter(Context context,List<ShareUser> users){
        this.context = context;
        this.users = users;
    }


    private  onDeleteDeviceClickListener   listener;


    public void setOnDeleteDeviceClickListener(onDeleteDeviceClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public DeviceShareAdapter.ShareViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

          View view  = LayoutInflater.from(context).inflate(R.layout.device_share_list_item,parent,false);
        return new ShareViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull ShareViewHolder holder, final int position) {

        holder.swipeMenuLayout.setIos(false);//设置是否开启IOS阻塞式交互
        holder.swipeMenuLayout.setLeftSwipe(true);//true往左滑动,false为往右侧滑动
        holder.swipeMenuLayout.setSwipeEnable(false);//设置侧滑功能开关
        String account="";
        if (!TextUtils.isEmpty(users.get(position).getPhone())){
            account=users.get(position).getPhone();
        }else {
            account=users.get(position).getEmail();
        }
        holder.tvPhone.setText(account);
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onDelete(position);
            }
        });
    }



    @Override
    public int getItemCount() {
        return users.size();
    }

    public  interface onDeleteDeviceClickListener{
        public void onDelete(int position);
    }

    class ShareViewHolder extends RecyclerView.ViewHolder {
        TextView tvPhone;
        SwipeMenuLayout swipeMenuLayout;
        Button  btnDelete;

        ShareViewHolder(View itemView) {
            super(itemView);
            tvPhone = itemView.findViewById(R.id.rv_device_share_list_item_title);
            swipeMenuLayout  = itemView.findViewById(R.id.sml_device_share_item);
            btnDelete  = itemView.findViewById(R.id.btn_device_share_item_delete);

        }
    }
}
